# -*- coding: utf-8 -*-

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
from flask.ext.mail import Mail
from config import MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD, ADMINS

__author__ = 'nibo'

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

lm = LoginManager(app)
lm.login_view = 'login'

if not app.debug:
    import logging
    from logging.handlers import SMTPHandler, RotatingFileHandler

    # 配置以邮件发送异常日志信息
    credentials = None
    if MAIL_USERNAME or MAIL_PASSWORD:
        credentials = (MAIL_USERNAME, MAIL_PASSWORD)
    mail_handler = SMTPHandler((MAIL_SERVER, MAIL_PORT), 'no-reply@' + MAIL_SERVER, ADMINS,
                               'system error', credentials)
    mail_handler.setLevel(logging.ERROR)
    # app.logger.addHandler(mail_handler)
    # end

    # 配置保存日志信息到文件
    file_handler = RotatingFileHandler('microblog.log', 'a', 1 * 1024 * 1024, 10, 'utf-8')
    file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)5s '
                                                '[%(filename)s:%(lineno)d]: %(message)s'))
    app.logger.setLevel(logging.INFO)
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)
    # end

    app.logger.info(u'启动microblog应用')

mail = Mail(app)

from app import views, models
